package com.longlong.backend.service;

import com.longlong.backend.dao.UserDao;
import com.longlong.backend.domain.User;
import com.longlong.backend.mapper.UserMapper;
import com.longlong.backend.util.MySpring;
import com.longlong.backend.util.MySqlSessionFactory;

import java.util.List;

/**
 * @author longlong
 * @date 2025/1/26
 */
public class UserService {

    /**
     * 自己写 UserDao 类
     */
    private UserDao userDao = MySpring.getBean("com.longlong.backend.dao.UserDao");

    /**
     * 只需要写一个 userMapper 接口
     * 动态代理
     */
    private UserMapper userMapper = MySqlSessionFactory.getSqlSession(true).getMapper(UserMapper.class);

    public void test() {

        User user = userMapper.selectById(1);
        System.out.println(user);

        List<User> users = userMapper.selectList();
        System.out.println(users);

    }

    public void testInsert(){
        System.out.println("start insert");
        User user = new User();
        user.setUserAccount("longlong");
        user.setUserPassword("123456");
        user.setUserName("龙龙");
        user.setUserAvatar("logo.jpg");
        user.setUserProfile("我是龙龙");
        userMapper.insert(user);
        System.out.println("end insert");
    }

    public void testUpdate(){
        System.out.println("start update");
        User user = new User();
        user.setId(1L);
        user.setUserAccount("longlong");
        user.setUserPassword("123456");
        user.setUserName("龙龙");
        user.setUserAvatar("logo.jpg");
        user.setUserProfile("我是龙龙");
        userMapper.updateById(user);
        System.out.println("end update");
    }
}
